package ruifeng.zcj.dig.ui.forum.category;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewHolder;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;
import ruifeng.zcj.dig.R;
import ruifeng.zcj.dig.groble.FragmentArgs;
import ruifeng.zcj.dig.groble.ToActivityUtil;
import ruifeng.zcj.dig.ui.base.RefreshRecyclerFragment;
import ruifeng.zcj.dig.ui.forum.category.bean.FirstGrade;
import ruifeng.zcj.dig.ui.forum.category.bean.TwoGrade;
import ruifeng.zcj.dig.ui.forum.talk.TalkActivity;

/**
 * 二级板块
 * Created by Administrator on 2016/10/19 0019.
 */
public class TwoGradeFragment extends RefreshRecyclerFragment {
    ChooseCategoryActivity mChooseCategoryActivity;
    private FirstGrade mFirstGrade;
    private List<TwoGrade> mMTwoGrades;
    private Integer isOhter;


    /**
     * 配置eventbus
     **/
    protected void initUIConfig(FragmentConfig fragmentConfig) {
        fragmentConfig.isApplyEventBus = true;
    }

    @Override
    protected void initFragmentConfig(View view) {
        super.initFragmentConfig(view);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        mChooseCategoryActivity = (ChooseCategoryActivity) mActivity;
    }

    @Override
    protected String getCacheKeyPrefix() {
        return "TwoGradeFragment";
    }

    @Override
    protected BGARecyclerViewAdapter getAdapter() {
        return new MomentAdapter(recyclerView);
    }

    @Override
    public void onRVItemClick(ViewGroup parent, View itemView, int position) {
        FragmentArgs args = new FragmentArgs();
        if (mFirstGrade == null) {
            args.add("gradest", FirstGrade.getGrade().get(0));
        } else {
            args.add("gradest", mFirstGrade);
        }
        args.add("twograds", mMTwoGrades.get(position));

        if (isOhter != null){// 开启一个acitivity
            ToActivityUtil.toNextActivity(getContext(), TalkActivity.class,"gradsBundle",args);
        }else {
            EventBus.getDefault().post(args);
        }
        mChooseCategoryActivity.finish();
    }

    @Override
    public boolean onRVItemLongClick(ViewGroup parent, View itemView, int position) {
        return false;
    }

    @Override
    protected void onUserVisible() {

    }


    /****
     * 网络请求数据具体的子类实现
     **/
    protected void sendRequestData() {
        addNewData(0);
    }

    /***
     * 不显示加载更多
     **/
    @Override
    protected boolean OnLoadingMore() {
        return false;
    }


    @Override
    protected long getAutoRefreshTime() {
        // 最新商品分类两小时刷新一次
        return 2 * 60 * 60;
    }


    public class MomentAdapter extends BGARecyclerViewAdapter<TwoGrade> {
        public MomentAdapter(RecyclerView recyclerView) {
            super(recyclerView, R.layout.textview);
        }

        @Override
        public void fillData(BGAViewHolderHelper helper, int position, TwoGrade moment) {
            /*if (TextUtils.isEmpty(moment.content)) {
                helper.setVisibility(R.id.tv_item_moment_content, View.GONE);
            } else {
                helper.setVisibility(R.id.tv_item_moment_content, View.VISIBLE);
                helper.setText(R.id.tv_item_moment_content, moment.content);
            }*/

            helper.setText(R.id.forum_plate, "" + moment.getName());

          /*
            /*TextView postingsname = helper.getView(R.id.forum_fragment_item_text_title);

            BGANinePhotoLayout ninePhotoLayout = helper.getView(R.id.npl_item_moment_photos);

            ninePhotoLayout.init(MomentListActivity.this);
            ninePhotoLayout.setDelegate(MomentListActivity.this);
            ninePhotoLayout.setData(moment.photos);*/
        }

        @Override
        public void onViewRecycled(BGARecyclerViewHolder holder) {
            super.onViewRecycled(holder);

           /* BGANinePhotoLayout ninePhotoLayout = holder.getViewHolderHelper().getView(R.id.npl_item_moment_photos);
            ninePhotoLayout.init(null);
            ninePhotoLayout.setDelegate(null);*/
        }
    }

    /**
     * 与发布者在同一个线程
     * 注意事项：
     * 1.事件被post后哪个事件的监听者会响应的问题，这个取决于你写的监听方法所跟的参数类型。
     * 2.你写的事件监听方法得是public修饰的，如果你写成private了，则不会收到消息。
     * 3.如果你所有的事件监听方法都不用public修饰（包括父类），则会直接报错：
     *
     * @param args 事件2
     */
    @Subscribe
    public void onEvent(FragmentArgs args) {
        mFirstGrade = (FirstGrade) args.get("grades");

        addNewData((Integer) args.get("postion"));
    }

    public void addNewData(Integer index) {
        mMomentAdapter.clear();
        mMTwoGrades = TwoGrade.getGrade(index.intValue());
        mMomentAdapter.addNewData(mMTwoGrades);
    }

    @Subscribe
    public void onEvent(Integer args) {
        isOhter = args;
      //  showSnackBar("从上个acitivity传过来的值"+args);
    }

}
